#include <iostream>
#include "../comm/httplib.h"
#include "oj_control.hpp"
using namespace httplib;
using namespace ns_control;

int main()
{
    Server svr;
    // http://39.104.201.129:8080/
    control ctrl;
    
    svr.Get("/all_question", [&ctrl](const Request &req, Response &resp)
            { 
                /*返回一张包含有所有列表的html网页*/
                std::string html;
                ctrl.AllQuestions(&html);
                resp.set_content(html, "text/html;charset=utf-8"); });

    svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp)
            {
        std::string number=req.matches[1];
        std::string html;
        ctrl.OneQuestion(number,&html);
        resp.set_content(html,"text/html;charset=utf-8"); });

    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp)
            {
                std::string number=req.matches[1];
                std::string result_json;
                ctrl.judge(number,req.body,&result_json);
                resp.set_content(result_json,"application/json;charset=utf-8");
                //resp.set_content("指定题目的判题："+number,"text/plain;charset=utf-8"); 
            });
          
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);
    return 0;
}
